/* Copyright 2012 Ali H. Caliskan.  All rights reserved.

   Sysmkr is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
	"fmt"
	"libmkr"
	"os"
	"strings"
)

var (
	SysCfg = true
)

func InfoGrp(grp string) {

	group := []string{}

	for pkgname, pkggrp := range libmkr.GetPkgInfo("grp") {

		if grp == pkggrp {

			group = append(group, pkgname)
		}
	}

	fmt.Println("info:", grp, "consists of", len(group), "packages")
}


func InfoOwn(file string) {

	for k, v := range libmkr.GetPkgInfo("list") {
		FileLists := strings.Split(v, "\n")

		for _, y := range FileLists {

			if file == y {

				s, _ := os.Stat(file)

				if !s.IsDir() {
				
					fmt.Println(k,y)
				}
			}
		}
	}
}


func InfoPkg(name string) {

	info := libmkr.GetInfoPkg(libmkr.GlobalSQLite, name)

	local := libmkr.GetInfoPkg(libmkr.LocalSQLite, name)

	if info[0] == "" {
		fmt.Println("info: package not available")
		os.Exit(0)
	}

	if local[0] != "" {

		fmt.Println(info[0], info[1], info[2], "(installed version:", local[1] + ")")

	} else {

		fmt.Println(info[0], info[1], info[2])
	}
}


func InfoSrc(name string) {

	info := libmkr.GetInfoSrc(libmkr.RecipeSQLite, name)

	fmt.Println(info[0], info[1], info[2], info[3], info[4])
}


func ListGrp(grp string) {

	for k, v := range libmkr.GetPkgInfo("grp") {

		if grp == v {
			fmt.Println(k)
		}
	}
}


func ListOwn(path string) {

	for k, v := range libmkr.GetPkgInfo("list") {
		FileLists := strings.Split(v, "\n")

		for _, y := range FileLists {

			if path == y {

				s, _ := os.Stat(path)

				if s.IsDir() {
				
					fmt.Println(k,y)
				}
			}
		}
	}
}


func ListPkg(pkg string) {

	pkglist := libmkr.GetInfoPkg(libmkr.GlobalSQLite, pkg)[3]

	fmt.Println(pkglist)
}


func ListSrc() {

	libmkr.GetSrcInfo(libmkr.RecipeSQLite)
}


func FreeGrp(grp string, root string, syscfg bool) {

	libmkr.RemovePackageGroup(grp, root, syscfg)
}


func FreePkg(pkg string, root string, syscfg bool) {

	libmkr.RemovePackage(pkg, root, syscfg)
}


func SyncGrp(grp string, root string, syscfg bool) {

	libmkr.InstallPackageGroup(grp, root, syscfg)
}


func SyncPkg(pkg string, root string, syscfg bool) {

	libmkr.InstallPackage(pkg, root, syscfg)
}


func Imgmkr(arg1 string, arg2 string) {

	args := []string{arg1, arg2}
	libmkr.Run("imgmkr", args...)
}


func Pkgmkr(src string, opt string) {

	args := []string{src, opt}
	libmkr.Run("pkgmkr", args...)
}


func Rcsmkr(name string, vers string, grp string, url string, path string) {

	args := []string{name, vers, grp, url, path}
	libmkr.Run("rcsmkr", args...)
}


func main() {
	flag := libmkr.GetArg(0)
	arg1 := libmkr.GetArg(1)
	arg2 := libmkr.GetArg(2)
	arg3 := libmkr.GetArg(3)
	arg4 := libmkr.GetArg(4)
	arg5 := libmkr.GetArg(5)

	args := libmkr.GetArgs()

	for i, arg := range args {

		if arg == "--grp-syscfg" {
			SysCfg = false

			if i == 2 {
				arg2 = ""
			}
		}
	}

	switch flag {
	case "info-grp":
		InfoGrp(arg1)
	case "info-own":
		InfoOwn(arg1)
	case "info-pkg":
		InfoPkg(arg1)
	case "info-src":
		InfoSrc(arg1)
	case "list-grp":
		ListGrp(arg1)
	case "list-own":
		ListOwn(arg1)
	case "list-pkg":
		ListPkg(arg1)
	case "list-src":
		ListSrc()
	case "free-grp":
		FreeGrp(arg1, arg2, SysCfg)
	case "free-pkg":
		FreePkg(arg1, arg2, SysCfg)
	case "sync-grp":
		SyncGrp(arg1, arg2, SysCfg)
	case "sync-pkg":
		SyncPkg(arg1, arg2, SysCfg)
	case "img":
		Imgmkr(arg1, arg2)
	case "pkg":
		Pkgmkr(arg1, arg2)
	case "rcs":
		Rcsmkr(arg1, arg2, arg3, arg4, arg5)
	case "":
		fmt.Println("warning: missing flag")
	}
}

